Skip to content

Conversation

iluuu1994
Copy link
Member

Fixes GH-20177

@iluuu1994
Copy link
Member Author

One side-effect in this PR is that this will also affect foreach ($this as $prop). https://3v4l.org/rCSNe#v8.4.13 Doesn't seem obvious what the "right" solution is here. Another approach might be to adjust just get_object_vars() to skip properties that have already been set, i.e. ones that were set by the parent class.

@nielsdos
Copy link
Member

Right. I understand the BC concern. Although I'd also expect that get_object_vars() and foreach() behaves consistently.

@iluuu1994
Copy link
Member Author

@nielsdos Right. Maybe it's ok to merge this for master then. The docs say:

https://www.php.net/manual/en/language.oop5.iterations.php

By default, all visible properties will be used for the iteration.

I guess the child property can be thought of as "reverse shadowed". Though I don't think these semantics were ever properly described or even considered.

@nielsdos
Copy link
Member

Agreed. This is kinda underspecified.

@iluuu1994
Copy link
Member Author

Pinging @bwoebi, as he has had opinions on previous, similar issues. ^^

Copy link
Member

@bwoebi bwoebi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks right to me. For any string $a, $this->$a and get_object_properties($this)[$a] should be equivalent.

I also welcome the foreach change, as otherwise foreach ($this as $prop => $foo) { $this->$prop = $foo + 1; } or something, will affect unexpected values. If you actually need to access only child-visible properties, you always can iterate over (static fn($obj) => get_object_properties($obj))($this) or simply (array)$this and work with the mangled property names.

Comment on lines +531 to +534
/* The property we found may be private if we have a private parent
* property and a protected child property, and we're accessing the
* property from the parent's scope. In that case we want to access
* the parent property, not child property. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have a shorter comment in the spirit of the two comments above.

"we are looking for a protected prop but found a private one of the same name but parent class"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified the comment before merging.

@iluuu1994 iluuu1994 closed this in 02c67b4 Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants